﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using DataLayer;
using System.Xml;

namespace DataImporterConsole.Screens {
    class ImportScreen : Screen {
        private List<char> validInput = new List<char>(new char[2] { '1', '2' });

        public ImportScreen() {
            this.Navigate = "import";
        }

        public override void Display() {
            string menu = "\t[1] Import File\r\n" +
                          "\t[2] Go back \r\n";
            Console.WriteLine(string.Format(screenbase, menu));
        }

        public override bool isValidInput(char cki) {
            return validInput.Contains(cki);
        }

        public override void Action(char cki) {
            switch (cki) {
                case '1':
                    importFile();
                    break;
                case '2':
                default:
                    break;
            }
        }

        private void importFile() {
            Console.WriteLine("Please enter Filename: ");
            string filename = Console.ReadLine();
                DataSpell[] i;
                try {
                    // A FileStream is needed to read the XML document.
                    FileStream fs = new FileStream(filename, FileMode.Open);
                    XmlReader reader = XmlReader.Create(fs);

                    XmlSerializer ser = new XmlSerializer(typeof(DataSpell[]));

                    // Declare an object variable of the type to be deserialized.


                    // Use the Deserialize method to restore the object's state.
                    i = (DataSpell[])ser.Deserialize(reader);
                    fs.Close();
                }
                catch (FileNotFoundException fe) {
                    Console.WriteLine("File Not found.");
                    return;
                }

            foreach (DataSpell spell in i) {
                Console.WriteLine(spell.Name);
            }
        }
    }
}
